package com.itwmk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwmk.entity.Employee;
import com.itwmk.service.impl.EmployeeService;
import com.itwmk.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登录功能
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String pwd=employee.getPassword();
        //进行md5加密;
        pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        //mybatisplus中的lambda表达式查询用户是否存在
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Employee> queryName = wrapper.eq(Employee::getUsername, employee.getUsername());
        //将查询到的用户封装
        Employee ep = employeeService.getOne(queryName);
        if (ep==null){
            return R.error("该用户不存在");
        }
        if (!ep.getPassword().equals(pwd)){
            return R.error("密码错误");
        }
        if (ep.getStatus()==0){
            return R.error("该账号已禁用");
        }
        //将用户id放入session中
        request.getSession().setAttribute("employee",ep.getId());
        return R.success(ep);
    }

    /**
     * 用户退出功能
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工功能
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> addUser(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工：{}",employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //Long eid = (Long)request.getSession().getAttribute("employee");
        //employee.setCreateUser(eid);
        //employee.setUpdateUser(eid);

        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> queryUser(int page, int pageSize, String name){

        Page <Employee>pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(name),Employee::getName,name);
        log.info("page:{}",StringUtils.hasLength(name));
        wrapper.orderByAsc(Employee::getName);
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }


    /**
     *更新用户信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));
        log.info(employee.toString());
        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    /**
     * 编辑用户信息功能
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> queryUserById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
